
#include "TimeLineEffect.h"
#include "SB.h"
#include "ScaleEffect.h"

TimeLineEffect::TimeLineEffect(bool _bEnabled, bool _bLoop) : SpriteEffect(0, 0, _bEnabled, _bLoop)
{
}

void TimeLineEffect::enableEffect(bool _bValue)
{
    if(_bValue)
    {
        for(int i=0; i<m_eEffects.size(); ++i)
        {
            m_bLaunched.at(i) = false;
            SpriteEffect* _eEffect = m_eEffects.at(i);
            _eEffect->reset();
        }
    }
}

void TimeLineEffect::updateEffect(float _fPerc)
{
    for(int i=0; i<m_eEffects.size(); ++i)
    {
        SpriteEffect* _eEffect = m_eEffects.at(i);
        float delay = m_fDelays.at(i);
        if(m_fElapsedTime >= delay && !m_bLaunched.at(i))
        {
            _eEffect->start(true);
            m_bLaunched.at(i) = true;
        }
    }
}

void TimeLineEffect::addEffect(SpriteEffect *_eEffect, float _fDelay)
{
    m_eEffects.push_back(_eEffect);
    m_fDelays.push_back(_fDelay);
    m_bLaunched.push_back(false);
    
    if(_eEffect->getDuration() + _fDelay > m_fDuration)
    {
        m_fDuration = _eEffect->getDuration() + _fDelay;
    }
}

void TimeLineEffect::setSpeed(float _fSpeed)
{
    m_fSpeed = _fSpeed;
    for(int i=0; i<m_eEffects.size(); ++i)
    {
        SpriteEffect* _eEffect = m_eEffects.at(i);
        _eEffect->setSpeed(_fSpeed);
    }   
}

void TimeLineEffect::addInEffect(RenderableEntity* _ent, float _fDelay)
{
    addEffect(new ScaleEffect(_ent, 0.10f, false, false, Vector2(0.0f, 0.0f), _ent->getScale() * 1.2f), _fDelay);
    addEffect(new ScaleEffect(_ent, 0.05f, false, false, _ent->getScale() * 1.2f, _ent->getScale()), _fDelay + 0.10f);
}

void TimeLineEffect::addOutEffect(RenderableEntity* _ent, float _fDelay)
{
    addEffect(new ScaleEffect(_ent, 0.05f, false, false, _ent->getScale(), _ent->getScale() * 1.2f), _fDelay);
    addEffect(new ScaleEffect(_ent, 0.10f, false, false, _ent->getScale() * 1.2f, Vector2(0.0f, 0.0f)), _fDelay + 0.05f);
}
